package com.jiyun.service;


import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jiyun.mapper.UserMapper;
import com.jiyun.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public String findAll() throws Exception {
        ObjectMapper objectMapper = new ObjectMapper();
        //查询所有
        String userListStr = (String) redisTemplate.boundValueOps("user_list").get();
        if(StringUtils.isEmpty(userListStr)){
            //查询数据库
            List<User> userList = userMapper.selectList(null);
            userListStr = objectMapper.writeValueAsString(userList);
            //存入redis
            redisTemplate.boundValueOps("user_list").set(userListStr);
            System.out.println("查询数据库。。。");
        }else{
            System.out.println("查询redis。。。");
        }
        return userListStr;
    }

}
